Page({
	data: {
		nickName: 'name',
		showModalInput: false,
		newUserName: '',
		inputShowed: true,
		avatarUrl: '/icon/225默认头像.png',
	},
	chooseImage: function (event) {
    let that = this;

    wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album'],
        success: function (res) {
            const tempFiles = res.tempFiles;
            that.setData({
                avatarUrl: tempFiles[0].tempFilePath
            });
            wx.setStorageSync('avatarUrl', tempFiles[0].tempFilePath);	
						console.log(tempFiles[0].tempFilePath);		
wx.request({
      url: 'http://localhost:8001/user/setUser',//连接服务器
      method: 'POST',
      data: {
        photo: tempFiles[0].tempFilePath
      },
      success: function(res) {
        console.log('头像修改成功');
      },
      fail: function(err) {
        console.error('头像修改失败', err);
      }
    });
        },
        fail: function (err) {
            console.error(err);
        }
    });
		
},
	onLoad() {
		// const EventChannel=this.getOpenerEventChannel()
		// EventChannel.emit('settingName',{name:this.nickName})
    let avatarUrl = wx.getStorageSync('avatarUrl');
		let nickName = wx.getStorageSync('SetnickName');
    if (avatarUrl) {
        this.setData({
            avatarUrl: avatarUrl
        });
    }
		if (nickName) {
        this.setData({
						nickName:nickName
        });
    }
	},
	showInputBox: function () {
		this.setData({
			showModalInput: true,
			newUserName: this.data.nickName
		});
		// console.log(this.data.newUserName)
	},
	confirmInput: function (res) {
		let newName = this.data.newUserName;
		console.log(newName)
		if (newName === '') {
			wx.showToast({
				title: '用户名不能为空',
				icon: 'none',
				duration: 2000
			});
			return;
		};
		this.setData({
			nickName: newName,
			showModalInput: false,
		});
		wx.setStorageSync('SetnickName', newName);
		console.log(158)
		console.log(this.data.nickName)
		wx.showToast({
			title: '用户名已修改',
			icon: 'success',
			duration: 2000
		});
		wx.request({
      url: 'http://localhost:8001/user/setUser',//连接服务器
      method: 'POST',
      data: {
        userName: newName
      },
      success: function(res) {
        console.log('名称修改成功');
      },
      fail: function(err) {
        console.error('名称修改失败', err);
      }
    });
	},

	cancelInput: function () {
		console.log('用户点击了取消输入');
		this.setData({
			showModalInput: false
		});
	},

	inputChange: function (e) {
		this.setData({
			newUserName: e.detail.value
		});
	},
	onSubmit(event) {
		const	{nickName} = event.detail.value
		this.setData({
			nickName:nickName
		})
		wx.setStorageSync('SetnickName', nickName);
	}
})